package com.softlab.retailwholesalesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softlab.retailwholesalesystem.entity.DetailOutInWarehouseEntity;
import com.softlab.retailwholesalesystem.entity.VO.OutInWarehouseGoodsVOEntity;
import com.softlab.retailwholesalesystem.mapper.DetailOutInWarehouseMapper;
import com.softlab.retailwholesalesystem.service.IDetailOutWarehouseService;
import com.softlab.retailwholesalesystem.service.IGoodsService;
import com.softlab.retailwholesalesystem.service.IRepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DetailOutInWarehouseServiceImpl extends ServiceImpl<DetailOutInWarehouseMapper, DetailOutInWarehouseEntity> implements IDetailOutWarehouseService {
    @Autowired
    private DetailOutInWarehouseMapper mapper;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IRepositoryService repositoryService;

    @Override
    public List<OutInWarehouseGoodsVOEntity> getMap(Long id) {
        QueryWrapper<DetailOutInWarehouseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("out_in_warehouse_id",id);
        wrapper.eq("is_delete",0);
        List<DetailOutInWarehouseEntity> entities = mapper.selectList(wrapper);
        List<OutInWarehouseGoodsVOEntity> res = new ArrayList<>();
        for (DetailOutInWarehouseEntity entity:
                entities) {
            res.add(transfer(entity));
        }


        return res;
    }

    @Override
    public OutInWarehouseGoodsVOEntity transfer(DetailOutInWarehouseEntity entity) {
        OutInWarehouseGoodsVOEntity outInWarehouseGoodsVOEntity = new OutInWarehouseGoodsVOEntity(entity);
        outInWarehouseGoodsVOEntity.setGoodsName(goodsService.getById(entity.getGoodsId()).getGoodsName());
        outInWarehouseGoodsVOEntity.setOutWarehouseName(repositoryService.getById(entity.getOutWarehouseId()).getRepositoryName());
        outInWarehouseGoodsVOEntity.setInWarehouseName(repositoryService.getById(entity.getInWarehouseId()).getRepositoryName());
        return outInWarehouseGoodsVOEntity;
    }
}
